// 用户 的仓库数据
import {state} from "../api/exception";
import {deleteToken, getToken, setToken} from "../utils/token";
import {http} from "../api/axios";
import {stringify} from "qs";
import {message} from "../utils/message";

let api = require("@/api").default;


export default {
    namespaced: true,
    state: {
        isLogin: false,
        isLoading: false,
        name:[],
        data:{
            avatar: require("@/assets/img/user/default-avatar.png"),
            nickname: "",
            email: ""
        },
    },
    getters:{

    },
    mutations:{
        /**
         * 设置加载状态
         */
        setIsLoading(state, payload){
            state.isLoading = payload;
        },
        /**
         * 设置登录状态
         */
        setIsLogin(state, payload){
            state.isLogin = payload;
        },
        /**
         * 设置数据
         */
        setData(state, payload){
            for(let field in state.data){
                payload[field] ? state.data[field] = payload[field] : null;
            }
        },
        /**
         * 清理data数据
         */
        clearData(state){
            state.data = {
                avatar: require("@/assets/img/user/default-avatar.png"),
                nickname: "",
                email: ""
            };
        },
        /**
         * 设置名称
         */
        setName(state, payload){
            state.name = payload;
        },
    },
    actions:{
        /**
         * 发送验证码
         */
        async getCode(context, payload){
            context.commit("setIsLoading", true);
            let isSuccess = false;
            // 调用api发送邮件
            let resp = await api.user.code(payload);
            if(resp.code === state.Success){
                isSuccess = true;
            } else {
                message.warning(resp.message)
            }

            context.commit("setIsLoading", false);

            return isSuccess;
        },
        /**
         * 注册
         */
        async reg(context, payload){
            context.commit("setIsLoading", true);
            // 调用注册方法
            let resp = await api.user.reg(payload);
            if(resp.code === state.Success){
                // 设置token
                setToken(resp.data);
            }

            context.commit("setIsLoading", false);

            // 返回数据
            return resp;
        },
        /**
         * 登录
         */
        async login(context, payload){
            context.commit("setIsLoading", true);
            // 调用注册方法
            let resp = await api.user.login(payload);
            if(resp.code === state.Success){
                // 设置token
                setToken(resp.data);
            }

            context.commit("setIsLoading", false);

            // 返回数据
            return resp;
        },
        /**
         * 登出
         */
        async logout(context, payload){
            context.commit("setIsLoading", true);

            let flag = false;
            let resp = await api.user.logout();
            if(resp.code == state.Success){
                // 删除token
                deleteToken();
                // 删除数据
                context.commit("clearData");
                // 设置登录状态
                context.commit("setIsLogin", false);
                flag = true;
            }

            context.commit("setIsLoading", false);
            return flag;
        },
        /**
         * 验证登录态
         */
        async validLogin(context, payload){
            context.commit("setIsLoading", true);

            if(getToken()){
                let resp = await api.user.isLogin();
                if(resp.code == state.Success){
                    context.commit("setIsLogin", true);
                }
            }else{
                context.commit("setIsLogin", false);
            }

            context.commit("setIsLoading", false);
        },
        /**
         * 获取用户信息
         */
        async getInfo(context, payload){
            context.commit("setIsLoading", true);

            let resp = await api.user.info(payload);
            if(resp.code == state.Success){
                resp.data.avatar ? resp.data.avatar = http + resp.data.avatar : null;
                context.commit("setData", resp.data);
            }

            context.commit("setIsLoading", false);

        },
        /**
         * 修改昵称
         */
        async changeNickname(context, payload){
            context.commit("setIsLoading", true);

            let isSuccess = true;

            let params = stringify({
                nickname: payload,
            });
            let resp = await api.user.nickname(params);
            if(resp.code === state.Success){
                message.success("修改昵称成功");
                context.commit("setData", {nickname: resp.data});
            }else{
                message.error(resp.message);
                isSuccess = false;
            }

            context.commit("setIsLoading", false);

            return isSuccess;
        },

        /**
         * 修改密码
         */
        async changePassword(context, payload){
            context.commit("setIsLoading", true);

            let isSuccess = true;

            let params = stringify({
                password: payload,
            });
            let resp = await api.user.password(params);
            if(resp.code === state.Success){
                message.success("修改密码成功");
            }else{
                message.error(resp.message);
                isSuccess = false;
            }

            context.commit("setIsLoading", false);

            return isSuccess;
        },

        /**
         * 上传头像
         */
        async uploadAvatar(context, payload){
            context.commit("setIsLoading", true);

            let isSuccess = true;

            let resp = await api.user.avatar(payload);
            if(resp.code === state.Success){
                message.success("头像上传成功");
                context.commit("setData", {avatar: http + resp.data});
            }else{
                message.error(resp.message);
                isSuccess = false;
            }

            context.commit("setIsLoading", false);

            return isSuccess;
        },
        /**
         * 获取所有用户昵称
         */
        async listNick(context, payload){
            context.commit("setIsLoading", true);

            let data = null;

            let resp = await api.user.allNick(payload);
            if(resp.code === state.Success){
                data = resp.data;
                await context.commit("setName", data);
            }else{
                message.error(resp.message);
            }

            context.commit("setIsLoading", false);

            return data;
        },
        /**
         * 获取当前用户id
         */
        async getId(context, payload){
            context.commit("setIsLoading", true);

            let data = null;

            let resp = await api.user.getId();
            if(resp.code === state.Success){
                data = resp.data;
            }else{
                message.error(resp.message);
            }

            context.commit("setIsLoading", false);

            return data;
        }
    },
};